/**************************************************************************************
* GameUI.java
* 
*************************************************************************************/
package creid.mythos.ui;

import java.awt.AWTKeyStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.KeyboardFocusManager;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Collections;
import java.util.HashMap;

import javax.swing.JFrame;

import creid.mythos.shared.GuiInterface;
import creid.mythos.shared.PlayerStatus;
import creid.mythos.shared.SpriteMap;

public class GameUI extends JFrame implements Runnable, GuiInterface
{
	//Constants/////////////////////////////////////////////////////////////////////////
	private static final long serialVersionUID = -663863509402542042L;

	//Mythos font
	public static final int FONT_SIZE = 13;
	public static final int FONT_HORIZONTAL_SPACING = 8;
	public static final int FONT_VERTICAL_SPACING = 13;
	public static final Font GAME_FONT = new Font(Font.MONOSPACED, Font.PLAIN, FONT_SIZE);
	
	//These constants account for the extra space taken up by the window border and imperfections in the size calculations
	public final static int FRAME_SIZE_X_MODIFIER = 15;
	public final static int FRAME_SIZE_Y_MODIFIER = 55;

	//Attributes////////////////////////////////////////////////////////////////////////
	
	//The colors
	static HashMap<Character, Color> colors = null;
	
	//Panels used in the gui
	private MapPanel mapPanel;
	/*private MenuPanel menuPanel;*/
	private MessagePanel messagePanel;
	
	private GamePanel sidePanel;
	private StatusPanel statusPanel;
	
	private TargetPanel targetPanel;
	
	private TextPanel titlePanel;
	
	//Note to quit the game
	private boolean quittingTime;

	//Constructors//////////////////////////////////////////////////////////////////////
	
	public GameUI(String title)
	{
		quittingTime = false; //Don't go yet
		
		//Load sprite and color data
		colors = ColorData.initColors();
		
		mapPanel = new MapPanel();
		/*menuPanel = new MenuPanel();*/
		messagePanel = new MessagePanel();
		sidePanel = new GamePanel();
		statusPanel = new StatusPanel();
		targetPanel = new TargetPanel();
		/*textPanel = new TextPanel();*/
		titlePanel = new TextPanel();
		//titlePanel.setText(titleText);
		
		//Place the panels
		setLayout(new BorderLayout());

		mapPanel.setVisible(false);
		add(mapPanel, BorderLayout.CENTER);

		titlePanel.setVisible(true);
		add(titlePanel, BorderLayout.CENTER);
		
		this.getContentPane().setBackground(colors.get(ColorData.BLACK));
		sidePanel.setVisible(false);
		sidePanel.setLayout(new BorderLayout());
		statusPanel.setVisible(false);
		sidePanel.add(statusPanel, BorderLayout.NORTH);
		sidePanel.add(targetPanel, BorderLayout.SOUTH);
		
		add(sidePanel, BorderLayout.WEST);
		
		messagePanel.setVisible(true);
		add(messagePanel, BorderLayout.SOUTH);
		
		//Misc gui stuff
		setTitle(title);
		setSize(976, 692); //TODO: Calculate this from the various panels.
		setResizable(false);
		
		//TODO: Replace with a CloseEvent that is handled by Mythos main class,
		//We would need to save the mythos before closing
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter()
		{
		    public void windowClosing(WindowEvent e)
		    {
		    	quittingTime = true;
		    }
		});
		
		
		this.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.<AWTKeyStroke> emptySet());
	}
	
	//Methods///////////////////////////////////////////////////////////////////////////

	@Override
	public void run()
	{
		setVisible(true);
	}

	public boolean isQuittingTime()
	{
		return quittingTime;
	}
	
	public void setQuittingTime(boolean quit)
	{
		quittingTime = quit;
	}
	
	public void showStatus()
	{
		targetPanel.setVisible(true);
		statusPanel.setVisible(true);
		statusPanel.setPreferredSize(new Dimension(StatusPanel.WIDTH, StatusPanel.HEIGHT));
		repaint();
	}
	
	public void hideStatus()
	{
		targetPanel.setVisible(false);
		statusPanel.setVisible(false);
		repaint();
	}
	
	public void hideTitle()
	{
		String[] blank = { "" };
		titlePanel.setText(blank);
		repaint();
	}
	
	public void showTitle(String[] titleText)
	{
		titlePanel.setText(titleText);
		//repaint();
		mapPanel.setVisible(false);
		//remove(mapPanel);
		//remove(titlePanel);
		add(titlePanel, BorderLayout.CENTER);
		titlePanel.setVisible(true);
		repaint();
	}
	
	public void showMap(SpriteMap map)
	{
		hideTitle();
		statusPanel.setVisible(true);
		sidePanel.setVisible(true);
		sidePanel.setSize(statusPanel.getWidth(), statusPanel.getHeight() + targetPanel.getHeight());
		remove(titlePanel);
		mapPanel.setMap(map);
		targetPanel.setMap(map);
		mapPanel.setVisible(true);
		add(mapPanel, BorderLayout.CENTER);
		repaint();
	}
	
	public void showMessage(String message)
	{
		if (!messagePanel.isVisible())
			messagePanel.setVisible(true);
		messagePanel.writeMessage(message);
		
		repaint();
	}
	
	public void showTransientMessage(String message)
	{
		if (!messagePanel.isVisible())
			messagePanel.setVisible(true);
		messagePanel.writeTransientMessage(message);
		repaint();
	}
	
	public void overwriteLastMessage(String message)
	{
		messagePanel.overwriteLastMessage(message);
		repaint();
	}
	
	public void clearMessages()
	{
		messagePanel.clearMessages();
	}
	
	public void updateTarget()
	{
		targetPanel.updateTarget();
		repaint();
	}
	
	public void updatePCStatus(PlayerStatus p)
	{
		statusPanel.updateState(p);
		repaint();
	}
}
